package com.admin.education.models;

import com.admin.education.models.Enum.QuestionType;
import com.admin.education.repositories.Identifiable;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Entity
@Table(name = "user_answer_sheet_question")
@EntityListeners(AuditingEntityListener.class)
@Data
public class UserAnswerSheetQuestion  implements Serializable,Identifiable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @Column(name="examination_questions")
    @NotBlank
    @Length(max=1024)
    @NotNull
    private String examinationQuestions;
    @Column(name = "question_type")
    @NotNull
    private QuestionType questionType;
    @OneToMany(mappedBy = "userAnswerSheetQuestion")
    @JSONField(serialize=false)
    private List<UserAnswerSheetAnswer> userAnswerSheetAnswers;
    @ManyToOne
    @NotNull
    @JSONField(serialize=false)
    private UserAnswerSheet userAnswerSheet;
    @OneToMany(mappedBy ="userAnswerSheetQuestion" )
    @JSONField(serialize=false)
    private List<UserAnswerSheetOwn> userAnswerSheetOwns;
    @Column(name="mark")
    @NotNull
    private BigDecimal mark;
    @OneToOne
    @NotNull
    @JSONField(serialize=false)
    private Subjects subjects;
    public void calculation(){
        if(null==this.userAnswerSheet.getTotalMark())
            this.userAnswerSheet.setTotalMark(new BigDecimal(0));
        switch (questionType){
            case JudgmentQuestion:
            case SingleChoiceQuestion:
                if(0==userAnswerSheetOwns.size())
                    break;
                if(1<userAnswerSheetOwns.size())
                    break;

                UserAnswerSheetOwn userAnswerSheetOwn = this.userAnswerSheetOwns.get(0);

                if(0!=this.userAnswerSheetAnswers.stream().filter(p->p.isRightAnswer()&&p.getAnswerDescription().equals(userAnswerSheetOwn.getAnswer())).count()){
                    this.userAnswerSheet.setTotalMark(this.userAnswerSheet.getTotalMark().add(this.mark));
                    this.userAnswerSheet.setCorrectNumberofQuestions(this.userAnswerSheet.getCorrectNumberofQuestions()+1);
                }

                break;
            case MultipleChoiceQuestion:
                if(0==userAnswerSheetOwns.size())
                    break;

                for (UserAnswerSheetAnswer userAnswerSheetAnswer : this.userAnswerSheetAnswers.stream().filter(p -> p.isRightAnswer()).collect(Collectors.toList())) {
                    if(0==this.userAnswerSheetOwns.stream().filter(p->p.getAnswer().equals(userAnswerSheetAnswer.getAnswerDescription())).count())
                        break;
                }
                this.userAnswerSheet.setTotalMark(this.userAnswerSheet.getTotalMark().add(this.mark));
                this.userAnswerSheet.setCorrectNumberofQuestions(this.userAnswerSheet.getCorrectNumberofQuestions()+1);
                break;
            case ShortAnswerQuestion:
                break;
        }

    }
}
